package com.webkit.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionContext implements Serializable {
	private static final long serialVersionUID = 3268310591678711007L;
	static final ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>();
	private RequestMap requestMap;
	private SessionMap sessionMap;
	private ApplicationContextMap contextMap;
	private ParameterMap parameterMap;
	private Map<String, Cookie> cookieMap;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected ServletContext context;

	protected static synchronized ActionContext createActionContext(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
		ActionContext ac = new ActionContext(context, request, response);
		actionContext.set(ac);
		return ac;
	}

	public static ActionContext getActionContext() {
		return (ActionContext) actionContext.get();
	}

	public String getRemoteAddress() {
		if (this.request == null)
			return null;
		return this.request.getRemoteAddr();
	}

	public HttpServletRequest getHttpServletRequest() {
		return this.request;
	}

	public HttpServletResponse getHttpServletResponse() {
		return this.response;
	}

	public ServletContext getServletContext() {
		return this.context;
	}

	private ActionContext(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
		this.context = context;
		init();
	}

	private void init() {
		this.requestMap = new RequestMap(this.request);
		this.sessionMap = new SessionMap(this.request);
		this.parameterMap = new ParameterMap(this.request);
		this.contextMap = new ApplicationContextMap(this.context);
		this.cookieMap = new HashMap();

		Cookie[] cookies = this.request.getCookies();
		if (cookies == null)
			return;
		for (Cookie cookie : cookies)
			this.cookieMap.put(cookie.getName(), cookie);
	}

	public OutputStream getOutputStream() throws IOException {
		return this.response.getOutputStream();
	}

	public void setResponseHeader(String key, String value) {
		this.response.addHeader(key, value);
	}

	public String getRequestHeader(String key) {
		return this.request.getHeader(key);
	}

	public void setCookie(String key, String value) {
		Cookie cookie = new Cookie(key, value);
		this.response.addCookie(cookie);
	}

	public void setCookie(Cookie cookie) {
		if (cookie != null)
			this.response.addCookie(cookie);
	}

	public Cookie getCookie(String key) {
		return (Cookie) this.cookieMap.get(key);
	}

	public String getCookieValue(String key) {
		Cookie cookie = (Cookie) this.cookieMap.get(key);
		if (cookie == null)
			return null;
		return cookie.getValue();
	}

	public Cookie[] getCookies() {
		return this.request.getCookies();
	}

	public Map<String, Object> getSession() {
		return this.sessionMap;
	}

	public Map<String, Object> getContext() {
		return this.contextMap;
	}

	public Map<String, Object> getRequest() {
		return this.requestMap;
	}

	public Map<String, Object> getParameters() {
		return this.parameterMap;
	}

	public void sendRedirect(String path) {
		if ((path.startsWith("http://")) && (path.startsWith("HTTP://")))
			sendRedirectExternal(path);
		try {
			this.response.sendRedirect(this.request.getContextPath() + path);
		} catch (IOException localIOException) {
		}
	}

	public void sendRedirectExternal(String url) {
		try {
			this.response.sendRedirect(url);
		} catch (IOException localIOException) {
		}
	}

	public String getApplicationPath() {
		String path = this.context.getRealPath("/");
		return path;
	}

	public String getApplicationClassPath() {
		String path = this.context.getRealPath("/WEB-INF/classes/");
		return path;
	}

	public InputStream getApplicationClassPathResourceAsStream(String name) {
		return this.context.getResourceAsStream("/WEB-INF/classes/" + name);
	}
}